%% -*- mode: erlang -*-

Patch = fun (Key, Value, Config) ->
                case lists:keysearch(Key, 1, Config) of
                    {value, {_, Org}} ->
                        lists:keyreplace(Key, 1, Config, {Key, Org ++ Value});
                    false ->
                        [{Key, Value}|Config]
                end
        end,

Merge = fun (NewCfg) ->
                PatchCfg =
                    case os:getenv("EXTRA_CONFIG") of
                        false ->
                            NewCfg;
                        File ->
                            {ok, ExtraCfg} = file:consult(File),
                            NewCfg ++ ExtraCfg
                    end,
                lists:foldl(
                  fun ({K, V}, C) -> Patch(K, V, C) end,
                  CONFIG, PatchCfg)
        end,

application:load(syntax_tools),
case application:get_key(syntax_tools, vsn) of
    {ok, Vsn} when "1.7" > Vsn ->
        Deps = [{merl, ".*",
                 {git, "git://github.com/erlydtl/merl.git", "750b09d44425f435ff579a4d28bf5844bb5b4ef1"},
                 [raw]}],
        PreHooks =
            [{"(linux|darwin|solaris)", compile, "make -C \"$REBAR_DEPS_DIR/merl\" all -W test"},
             {"(freebsd|netbsd|openbsd)", compile, "gmake -C \"$REBAR_DEPS_DIR/merl\" all"},
             {"win32", compile, "make -C \"%REBAR_DEPS_DIR%/merl\" all -W test"},
             {"(linux|darwin|solaris)", eunit, "make -C \"$REBAR_DEPS_DIR/merl\" test"},
             {"(freebsd|netbsd|openbsd)", eunit, "gmake -C \"$REBAR_DEPS_DIR/merl\" test"},
             {"win32", eunit, "make -C \"%REBAR_DEPS_DIR%/merl\" test"}],

        Merge([{deps, Deps},
               {pre_hooks, PreHooks},
               {erl_opts, [{d, 'MERL_DEP'}]}]);
    _ ->
        Merge([])
end.
